package com.docHunt.DAO.DAOImpl;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.hibernate.transform.Transformers;
import org.springframework.beans.BeanUtils;

import com.docHunt.DAO.UserDAO;
import com.docHunt.DTO.UserDTO;
import com.docHunt.model.TblUser;

public class UserDAOImpl extends BaseDAOImpl implements UserDAO {

	/*
	 * public DoctorDTO getDoctor() {
	 * 
	 * List<DoctorDTO> doctorDTOList = null;
	 * 
	 * try { // Create criteria DetachedCriteria docCriteria =
	 * DetachedCriteria.forClass(TblDoctor.class);
	 * docCriteria.setProjection(Projections.projectionList()
	 * .add(Projections.property("doctorId"),"doctorId")
	 * .add(Projections.property("firstName"),"firstName")
	 * .add(Projections.property("lastName"),"lastName"));
	 * docCriteria.addOrder(Order.asc("doctorId"));
	 * 
	 * docCriteria.setResultTransformer(Transformers.aliasToBean(DoctorDTO.class)
	 * );
	 * 
	 * // Fetch the list doctorDTOList = (List<DoctorDTO>)
	 * super.getHibernateTemplate().findByCriteria(docCriteria);
	 * 
	 * //BeanUtils.copyProperties(tblDoctorList.get(0), doctorDtoList);
	 * 
	 * } catch (Exception e) {
	 * logger.error("UserDAOImpl :: Exception in method getDoctor: ", e); }
	 * 
	 * return doctorDTOList.get(0); }
	 */

	public boolean addUser(UserDTO userDTO) {

		TblUser tblUser = new TblUser();

		try {
			BeanUtils.copyProperties(userDTO, tblUser);

			super.getHibernateTemplate().save(tblUser);

		} catch (Exception e) {
			logger.error("UserDAOImpl :: Exception in method addUser: ", e);
			return false;
		}

		return true;
	}

	public UserDTO getUser(String loginId) {

		UserDTO userDTO = null;
		try {

			DetachedCriteria userCriteria = DetachedCriteria.forClass(TblUser.class);
			
			userCriteria.setProjection(Projections.projectionList()
					.add(Projections.property("userId"),"userId")
					.add(Projections.property("loginId"),"loginId")
					.add(Projections.property("password"),"password")
					.add(Projections.property("userType"),"userType")
					.add(Projections.property("verificationCode"),"verificationCode")
					.add(Projections.property("verificationFlag"),"verificationFlag")
					.add(Projections.property("createdDate"),"createdDate")
				);
			
			userCriteria.add(Restrictions.eq("this.loginId", loginId));
			
			userCriteria.setResultTransformer(Transformers.aliasToBean(UserDTO.class));

			userDTO = (UserDTO) super.getHibernateTemplate().findByCriteria(userCriteria).get(0);

		} catch (Exception e) {
			logger.error("UserDAOImpl :: Exception in method getUser: ", e);
		}

		return userDTO;
	}
}
